#pragma once
#include <windows.h>
#include "Data.h"
#include <list>
#include <iterator>
using namespace std;

class ThreadPool
{
private:
	int maxThreads;
	int minThreads;
	int threadSpanTime;
	int threadWaitTime;
	list<ThreadData *> idleThreads;
	list<ThreadData *> runningThreads;
	list<Runnable *> toExecutedItems;
	CRITICAL_SECTION idleThreadsCriticalSection;
	CRITICAL_SECTION runningThreadsCriticalSection;
	CRITICAL_SECTION toExecutedItemsCriticalSection;
private:
	static DWORD WINAPI ThreadPoolFun(void *);
	void Initialize(int maxThreads, int initThreads);
	ThreadData * GetOneThread();
	BOOL IfCloseThread(ThreadData * threadData);
	ThreadData * CreateOneThread();
public:
	ThreadPool();
	ThreadPool(int maxThreads, int minThreads);
	~ThreadPool();
	void Start(Runnable * runnable);
};